<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\MessageBag;

use App\Models\Admin;
use App\Models\Group;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{

    //机构后台登录接口
    function postProject(Request $request){ 
    	return $this->Login($request,config('constants.ADMIN_PROJECT_TYPE'));
    }
    //总后台登录接口
    function postBackstage(){
        return $this->Login($request,config('constants.ADMIN_ADMIN_TYPE'));
    }
    // 登录
    private function Login($request,$type){
        $data=['username'=>'required','password'=>'required'];
        //验证
        $validator = Validator::make($request->all(),$data);
        if ($validator->fails()) {
            return response()->json(['code'=>config('constants.API_NULL_CODE'),'message'=>$validator->errors()->first()]);
        }
        $user=Admin::where('name',$request->input('username'))->first();
        if($user){
            if($user->type!=$type){
                return response()->json(['code'=>config('constants.API_NOT_EXISTS_CODE'),'message'=>$validator->errors()->first()]);
            }
            $group=Group::where('id',$user->group_id)->first();
            if($group->status==0){
                return response()->json(['code'=>config('constants.API_ADMIN_STATUS_ADOPT_CODE'),'message'=>config('constants.API_ADMIN_STATUS_ADOPT')]);
            }else if($group->status==2){
                return response()->json(['code'=>config('constants.API_ADMIN_NO_ADOPT_CODE'),'message'=>config('constants.API_ADMIN_NO_ADOPT')]);
            }
            $password=hash('sha256',$request->input('password').config('constants.PWDSALT'));
            if($user->password==$password){
                return response()->json(['code'=>config('constants.API_SUCCESS_CODE'),'message'=>config('constants.API_SUCCESS'),'token'=>Crypt::encrypt(json_encode($user))]);
            }
            return response()->json(['code'=>config('constants.API_ADMIN_PASSWORD_CODE'),'message'=>$validator->errors()->first()]);
        }
        return response()->json(['code'=>config('constants.API_ADMIN_USERNAME_CODE'),'message'=>$validator->errors()->first()]);
    }
    // 机构后台注册接口
    function postRegister(Request $request){
        $data=[
            'username'=>'required',
            'password'=>'required',
            'name'=>'required',
            'liable'=>'required',
            'start_time'=>'required',
            'province'=>'required',
            'city'=>'required',
            'location'=>'required',
            'website'=>'required',
            'card'=>'required',
            'content'=>'required',
            'bank_number'=>'required',
            'account'=>'required',
            'bank_name'=>'required',
            'mobile'=>'required',
            'email'=>'required',
            ];
        //验证
        $validator = Validator::make($request->all(),$data);
        if ($validator->fails()) {
            return response()->json(['code'=>config('constants.API_NULL_CODE'),'message'=>$validator->errors()->first()]);
        }
        $user=Admin::where('name',$request->input('username'))->first();
        if($user){
            return response()->json(['code'=>config('constants.API_ADMIN_REPEAT_CODE'),'message'=>config('constants.API_EXISTS')]);
        }
        // return $request->input('password');
        $data=array(
            'name' => $request->input('name'), 
            'liable' => $request->input('liable'), 
            'start_time' => $request->input('start_time'), 
            'province' => $request->input('province'), 
            'city' => $request->input('city'), 
            'location' => $request->input('location'), 
            'website' => $request->input('website'), 
            'card' => $request->input('card'), 
            'content' => $request->input('content'), 
            'bank_number' => $request->input('bank_number'), 
            'account' => $request->input('account'), 
            'bank_name' => $request->input('bank_name'), 
            'mobile' => $request->input('mobile'), 
            'email' => $request->input('email'), 
            );
        $temp=Group::create($data);
        $adminData=array(
            'name' => $request->input('username'), 
            'password' => hash('sha256',$request->input('password').config('constants.PWDSALT')), 
            'group_id'=>$temp->id,
            'type'=>config('constants.ADMIN_GROUP_TYPE'),
            );
        $temp=Admin::create($adminData);
        if($temp){
            return response()->json(['code'=>config('constants.API_SUCCESS_CODE'),'message'=>config('constants.API_SUCCESS')]);
        }
        return response()->json(['code'=>config('constants.API_PARAM_ERROR_CODE'),'message'=>$validator->errors()->first()]);
    }
}
